<!DOCTYPE html>
<html debug="true">
  <head>
    <script type="text/javascript" src="../../../../../openlayers/lib/OpenLayers.js"></script>
    <script type="text/javascript" src="../../../../../ext/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="../../../../../ext/ext-all-debug.js"></script>
    <script type="text/javascript" src="../../../../lib/GeoExt.js"></script>

    <script type="text/javascript">
        var printCapabilities = {"scales":[{"name":"1:25,000","value":"25000"},{"name":"1:50,000","value":"50000"},{"name":"1:100,000","value":"100000"},{"name":"1:200,000","value":"200000"},{"name":"1:500,000","value":"500000"},{"name":"1:1,000,000","value":"1000000"},{"name":"1:2,000,000","value":"2000000"},{"name":"1:4,000,000","value":"4000000"}],"dpis":[{"name":"75","value":"75"},{"name":"150","value":"150"},{"name":"300","value":"300"}],"layouts":[{"name":"A4 portrait","map":{"width":440,"height":483},"rotation":true},{"name":"Legal","map":{"width":440,"height":483},"rotation":false}],"printURL":"http://demo.opengeo.org/geoserver/pdf/print.pdf","createURL":"http://demo.opengeo.org/geoserver/pdf/create.json"};
       
        function test_ctor(t) {
            t.plan(2);

            var printProvider = new GeoExt.data.PrintProvider({
                capabilities: printCapabilities
            })
            
            var printExtent = new GeoExt.plugins.PrintExtent({
                printProvider: printProvider
            });
            
            t.ok(printExtent.printProvider === printProvider,
                 "ctor sets printProvider in the instance");
            t.ok(printExtent.pages[0] instanceof GeoExt.data.PrintPage,
                 "ctor creates a PrintPage");
        }
        
        function test_init(t) {
            t.plan(2);

            var printExtent = new GeoExt.plugins.PrintExtent({
                printProvider: new GeoExt.data.PrintProvider({
                    capabilities: printCapabilities
                })
            });

            var mapPanel = new GeoExt.MapPanel({
                renderTo: "mappanel",
                width: 256,
                height: 256,
                map: {controls: []},
                layers: [new OpenLayers.Layer("layer", {isBaseLayer: true})],
                center: [146.56, -41.56],
                zoom: 7,
                plugins: [printExtent]
            });
            
            t.ok(printExtent.map === mapPanel.map,
                 "init sets mapPanel");
            t.ok(mapPanel.hasListener("destroy"),
                 "plugin listens to the panel destroy event");

            mapPanel.destroy();
         }

        function test_destroy(t) {
            t.plan(3);

            var printExtent = new GeoExt.plugins.PrintExtent({
                printProvider: new GeoExt.data.PrintProvider({
                    capabilities: printCapabilities
                })
            });

            var mapPanel = new GeoExt.MapPanel({
                renderTo: "mappanel",
                width: 256,
                height: 256,
                map: {controls: []},
                layers: [new OpenLayers.Layer("layer", {isBaseLayer: true})],
                center: [146.56, -41.56],
                zoom: 7,
                plugins: [printExtent]
            });

            mapPanel.destroy();
            
            t.eq(printExtent.layer, undefined,
                 "layer destroyed");
            t.eq(printExtent.control, undefined,
                 "control destroyed.");
            t.eq(printExtent.mapPanel, null,
                 "mapPanel set to null");
        }

         function test_setUp(t) {
            t.plan(4);

            var printExtent = new GeoExt.plugins.PrintExtent({
                printProvider: new GeoExt.data.PrintProvider({
                    capabilities: printCapabilities
                })
            });

            var mapPanel = new GeoExt.MapPanel({
                renderTo: "mappanel",
                width: 256,
                height: 256,
                map: {controls: []},
                layers: [new OpenLayers.Layer("layer", {isBaseLayer: true})],
                center: [146.56, -41.56],
                zoom: 7,
                plugins: [printExtent]
            });

            t.ok(printExtent.pages[0].center.equals(mapPanel.map.getCenter()),
                 "print page centered to map center");
            t.ok(printExtent.layer.map == mapPanel.map,
                 "auto-generated layer added to map.");
            t.ok(printExtent.pages[0].feature.layer == printExtent.layer,
                 "extent feature was added to the layer.");
            t.ok(printExtent.control.map == mapPanel.map,
                 "control created and added to map.");

            mapPanel.destroy();
        }

        function test_tearDown(t) {
            t.plan(3);
 
            var printExtent = new GeoExt.plugins.PrintExtent({
                printProvider: new GeoExt.data.PrintProvider({
                    capabilities: printCapabilities
                })
            });

            var mapPanel = new GeoExt.MapPanel({
                renderTo: "mappanel",
                width: 256,
                height: 256,
                map: {controls: []},
                layers: [
                    new OpenLayers.Layer("layer", {isBaseLayer: true})
                ],
                center: [146.56, -41.56],
                zoom: 7,
                plugins: [printExtent]
            });

            printExtent.tearDown();
            
            t.eq(mapPanel.map.layers.length, 1,
                 "layer was removed.");
            t.eq(mapPanel.map.controls.length, 0,
                 "control was removed.");
            t.eq(printExtent.pages[0].feature.layer, null,
                 "feature was removed from the layer.")

            mapPanel.destroy();
        }
    </script>
  <body>
    <div id="mappanel"></div>
  </body>
</html>
